//
//  ViewController.m
//  UITableView
//
//  Created by apple on 15/9/10.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    
    NSIndexPath *_deletaIndexPath;
    
    UITableView *_tableView;
    
    UITextField *_textField;
    
    NSMutableArray *_array;
}


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
#pragma mark - 标题
    
    CGSize size = self.view.frame.size;
    
    //状态栏高20px
    
    //确定标题的位置；
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, size.width, 40)];
    
    //标题的内容
    label.text = @"我的任务列表";
    
    //标题的字体的大小
    label.font = [UIFont boldSystemFontOfSize:25];
    
    //标题按什么样式对齐
    label.textAlignment = NSTextAlignmentCenter;
    
    //显示label
    [self.view addSubview:label];
    
#pragma mark - 保存按钮
    
    UIButton *but = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [but setTitle:@"保存" forState:UIControlStateNormal];
    
    [but addTarget:self action:@selector(didSaveClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    but.frame = CGRectMake(size.width - 50, 20, 40, 40);
    
    [self.view addSubview:but];
    
#pragma mark - 输入框
    
    CGRect rect;
    rect.origin = CGPointMake(15, 70);
    rect.size = CGSizeMake(size.width - 30, 30);
    
    _textField = [[UITextField alloc] initWithFrame:rect];
    
    //设置输入框提示内容；
    _textField.placeholder = @"请输入任务：";
    
    [self.view addSubview:_textField];
    
#pragma mark - 表格 - UITableView
    
    CGRect tableRect = CGRectMake(0, 110, size.width, size.height - 110);
    
//    UITableView对象有两种样式：一种为普通样式，一种为分组样式；
    
    _tableView = [[UITableView alloc] initWithFrame:tableRect style:UITableViewStylePlain];
    
    //注册UITableView 使用的cell
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    _tableView.dataSource = self;
    
    _tableView.delegate = self;
    
    [self.view addSubview:_tableView];
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didSaveClicked:(UIButton *)sender
{
    // 删除字符串前后的空白字符
    NSString *str = [_textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    if (str.length == 0)
    {
        return;
    }
    if (_array == nil)
    {
        _array =[NSMutableArray array];
    }
    [_array addObject:str];
    
    _textField.text = nil;
    
    //刷新表格
    [_tableView reloadData];
}

    //每个分组里面有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}

    //每一行cell的显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //找tableView获取cell对象
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    //indexpath 行显示数组的第indexpath个元素；
    
    cell.textLabel.text = _array[indexPath.row];
    
    return cell;
    
}

    //开始执行编辑操作，如删除和插入等操作，

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        _deletaIndexPath = indexPath;
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定要删除此次的内容吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        
        //弹出提示框
        [alertView show];
    }
}

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        [_array removeObjectAtIndex:_deletaIndexPath.row];
        
        [_tableView deleteRowsAtIndexPaths:@[_deletaIndexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    else
    {
        //刷新一个cell使它恢复正常状态
        
        [_tableView reloadRowsAtIndexPaths:@[_deletaIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
        _deletaIndexPath = nil;
    }
}

//默认cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

//获取用户选择

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
